package com.hengpeng.itfinoss.service.article.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleDiscuss;
import com.hengpeng.itfinbase.persist.ArticleDiscuss.DiscussState;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.article.ArticleService;

/**
 * <pre>
 * 新闻管理服务接口实现
 * </pre>
 * @author LanMingMing
 * @version 1.0, 2011-9-5
 */
@Service
@Lazy
public class ArticleServiceImpl implements ArticleService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<Article>> findAll()
	{
		List<Article> list = serviceFactory.getPersistService().list(Article.class);
		return new ServiceResult<List<Article>>(list);
	}

	@Override
	public ServiceResult<Article> get(Long id)
	{
		Article article = serviceFactory.getPersistService().get(Article.class, id);
		return new ServiceResult<Article>(article);
	}

	@Override
	public ServiceResult<Article> load(Long id)
	{
		Article article = serviceFactory.getPersistService().get(Article.class, id);
		return new ServiceResult<Article>(article);
	}

	@Override
	public ServiceResult<Article> modfiy(Article article)
	{
		Article obj = serviceFactory.getPersistService().update(article);
		return new ServiceResult<Article>(obj);
	}

	@Override
	public ServiceResult<Article> remove(Set<Object> ids)
	{
		for (Object id : ids)
		{
			serviceFactory.getPersistService().delete(Article.class, id);
		}
		return new ServiceResult<Article>();
	}

	@Override
	public ServiceResult<Article> save(Article article)
	{
		Article obj = serviceFactory.getPersistService().save(article);
		return new ServiceResult<Article>(obj);
	}

	@Override
	public ServiceResult<Long> findArticleCount(String appUserId, String author, Long[] categoryIds, String content,
			String fullTitle, BoolValue isAudited, BoolValue isHignlight, BoolValue isTop, String source,
			Date updateTime1, Date updateTime2, Date setTopEndTime1, Date setTopEndTime2)
	{
		DynamicQuery pagination = new DynamicQuery(Article.class);
		pagination.setIsSearchTotalCount(true);
		if (!StringHelper.isEmpty(appUserId))
		{
			pagination.eq("appUserId", appUserId);
		}
		if (!StringHelper.isEmpty(author))
		{
			pagination.like("author", "%" + author + "%");
		}
		if (categoryIds != null && categoryIds.length > 0)
		{
			pagination.inArray("category.id", categoryIds);
		}
		if (!StringHelper.isEmpty(content))
		{
			pagination.like("content", "%" + content + "%");
		}
		if (!StringHelper.isEmpty(fullTitle))
		{
			pagination.like("fullTitle", "%" + fullTitle + "%");
		}
		if (isAudited != null)
		{
			pagination.eq("isAudited", isAudited);
		}
		if (isHignlight != null)
		{
			pagination.eq("isHignlight", isHignlight);
		}
		if (isTop != null)
		{
			pagination.eq("isTop", isTop);
		}
		if (!StringHelper.isEmpty(source))
		{
			pagination.like("source", "%" + source + "%");
		}
		if (updateTime1 != null && updateTime2 != null)
		{
			pagination.between("updateTime", updateTime1, updateTime2);
		}
		if (setTopEndTime1 != null && setTopEndTime2 != null)
		{
			pagination.between("setTopEndTime", setTopEndTime1, setTopEndTime2);
		}
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(pagination, null));
	}

	public QueryResult<Article> findArticle(long id, String appUserId, String author, Long[] categoryIds,
			String content, String fullTitle, BoolValue isAudited, BoolValue isHignlight, BoolValue isTop,
			String source, Date updateTime1, Date updateTime2, Date setTopEndTime1, Date setTopEndTime2,
			Integer pageIndex, Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery pagination = new DynamicQuery(Article.class);
		pagination.setIsSearchTotalCount(true);
		if (id != -1)
		{
			pagination.eq("id", id);
		}
		if (!StringHelper.isEmpty(appUserId))
		{
			pagination.eq("appUserId", appUserId);
		}
		if (!StringHelper.isEmpty(author))
		{
			pagination.like("author", "%" + author + "%");
		}
		if (categoryIds != null && categoryIds.length > 0)
		{
			pagination.inArray("category.id", categoryIds);
		}
		if (!StringHelper.isEmpty(content))
		{
			pagination.like("content", "%" + content + "%");
		}
		if (!StringHelper.isEmpty(fullTitle))
		{
			pagination.like("fullTitle", "%" + fullTitle + "%");
		}
		if (isAudited != null)
		{
			pagination.eq("isAudited", isAudited);
		}
		if (isHignlight != null)
		{
			pagination.eq("isHignlight", isHignlight);
		}
		if (isTop != null)
		{
			pagination.eq("isTop", isTop);
		}
		if (!StringHelper.isEmpty(source))
		{
			pagination.like("source", "%" + source + "%");
		}
		if (updateTime1 != null && updateTime2 != null)
		{
			pagination.between("updateTime", updateTime1, updateTime2);
		}
		if (setTopEndTime1 != null && setTopEndTime2 != null)
		{
			pagination.between("setTopEndTime", setTopEndTime1, setTopEndTime2);
		}
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					pagination.desc(entry.getKey());
				}
				else
				{
					pagination.asc(entry.getKey());
				}
			}
		}
		return serviceFactory.getPersistService().findWithPage(Article.class, pagination);
	}

	@Override
	public ServiceResult<List<Article>> findArticlePage(String appUserId, String author, Long[] categoryIds,
			String content, String fullTitle, BoolValue isAudited, BoolValue isHignlight, BoolValue isTop,
			String source, Date updateTime1, Date updateTime2, Date setTopEndTime1, Date setTopEndTime2,
			Integer pageIndex, Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery pagination = new DynamicQuery(Article.class);
		if (!StringHelper.isEmpty(appUserId))
		{
			pagination.eq("appUserId", appUserId);
		}
		if (!StringHelper.isEmpty(author))
		{
			pagination.like("author", "%" + author + "%");
		}
		if (categoryIds != null && categoryIds.length > 0)
		{
			pagination.inArray("category.id", categoryIds);
		}
		if (!StringHelper.isEmpty(content))
		{
			pagination.like("content", "%" + content + "%");
		}
		if (!StringHelper.isEmpty(fullTitle))
		{
			pagination.like("fullTitle", "%" + fullTitle + "%");
		}
		if (isAudited != null)
		{
			pagination.eq("isAudited", isAudited);
		}
		if (isHignlight != null)
		{
			pagination.eq("isHignlight", isHignlight);
		}
		if (isTop != null)
		{
			pagination.eq("isTop", isTop);
		}
		if (!StringHelper.isEmpty(source))
		{
			pagination.like("source", "%" + source + "%");
		}
		if (updateTime1 != null && updateTime2 != null)
		{
			pagination.between("updateTime", updateTime1, updateTime2);
		}
		if (setTopEndTime1 != null && setTopEndTime2 != null)
		{
			pagination.between("setTopEndTime", setTopEndTime1, setTopEndTime2);
		}
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					pagination.desc(entry.getKey());
				}
				else
				{
					pagination.asc(entry.getKey());
				}
			}
		}
		List<Article> articles = serviceFactory.getPersistService().find(Article.class, pagination);
		return new ServiceResult<List<Article>>(articles);
	}

	@Override
	public ServiceResult<List<Article>> findArticleByCategoryIds(Long[] ids)
	{
		DynamicQuery pagination = new DynamicQuery(Article.class);
		pagination.inArray("category.id", ids);
		List<Article> articles = serviceFactory.getPersistService().find(Article.class, pagination);
		return new ServiceResult<List<Article>>(articles);
	}

	@Override
	public <T extends AbstractEntity> ServiceResult<T> batchRemove(String field, Object[] values, Class<T> clz)
	{
		DynamicQuery query = new DynamicQuery(clz);
		query.inArray(field, values);
		List<T> list = serviceFactory.getPersistService().find(clz, query);
		for (T t : list)
		{
			serviceFactory.getPersistService().delete(clz, t.getId());
		}
		return new ServiceResult<T>();
	}

	@Override
	public <T extends AbstractEntity> List<T> findUnionArticleById(String field, Object[] values, Class<T> type)
	{
		DynamicQuery query = new DynamicQuery(type);
		query.inArray(field, values);
		return serviceFactory.getPersistService().find(type, query);
	}

	@Override
	public QueryResult<ArticleDiscuss> findDicuss(long id, String content, String userName, Date publishTime_s,
			Date publishTime_e, String state, String auditor, Date auditTime_s, Date auditTime_e, int currentPage,
			int pageSize)
	{
		DynamicQuery query = new DynamicQuery(ArticleDiscuss.class);
		if (id != -1)
		{
			query.eq("articleId", id);
		}
		if (!StringHelper.isEmpty(content))
		{
			query.like("discussContent", "%" + content + "%");
		}
		if (!StringHelper.isEmpty(userName))
		{
			query.like("userName", "%" + userName + "%");
		}
		if (publishTime_s != null && publishTime_e != null)
		{
			query.between("publishTime", publishTime_s, publishTime_e);
		}
		if (!"-1".equals(state))
		{
			query.eq("state", DiscussState.valueOf(state));
		}
		if (!StringHelper.isEmpty(auditor))
		{
			query.like("auditor", "%" + auditor + "%");
		}
		if (auditTime_s != null && auditTime_e != null)
		{
			query.between("auditTime", auditTime_s, auditTime_e);
		}
		query.setIsSearchTotalCount(true);
		query.setPageIndex(currentPage);
		query.setPageSize(pageSize);
		query.desc("publishTime");
		query.desc("state");
		return serviceFactory.getPersistService().findWithPage(ArticleDiscuss.class, query);
	}

	@Override
	public List<ArticleDiscuss> findDicuss(Long[] ids)
	{
		DynamicQuery query = new DynamicQuery(ArticleDiscuss.class);
		query.inArray("id", ids);
		return serviceFactory.getPersistService().find(ArticleDiscuss.class, query);
	}
}
